const { db, command, cloud } = require(':share/webServer')
const useMidware = require(':utils/useMidware')
const { genNumberByWeight } = require(':utils/randomNum')
const checkJson = require(':utils/checkJson')

async function checkData(ctx, next)
{
    const scheam = {
        type: 'object',
        properties: {
            roulette_id: { type: 'number' }
        },
        required: ['roulette_id']
    }
    checkJson(ctx.request.body, scheam)
    await next()
}

async function lottery(ctx, next)
{
    let { roulette_id } = ctx.request.body

    let rsp = await db.collection('Roulettes').where({
        roulette_id
    }).get()
    let roulette = rsp.data[0]

    let now = new Date().valueOf()
    if (now >= roulette.release_prize_at && now <= roulette.release_prize_end_at) //在放奖时间内
    {
        ctx.body = {
            prize_id: genNumberByWeight(roulette.prize_ids, roulette.release_prize_probability)
        }
    }
    else
    {
        ctx.body = {
            prize_id: genNumberByWeight(roulette.prize_ids, roulette.prize_probability)
        }
    }
}


useMidware.post('/modules/lotteryPage/lottery', checkData) //抽奖
useMidware.post('/modules/lotteryPage/lottery', lottery) //抽奖